#include <cstdio>
#include <vector>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
typedef pair<int, int> pii;

#define MAXN 105
#define ABS(a) (a>0?a:-a)
#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
#define INF 0x3F3F3F3F

vector<pii> G[MAXN];
int VALUE[MAXN];
int LEVEL[MAXN];
int M, N;

int dis[MAXN];
bool vis[MAXN];


void SPFA(int start, int end) {
	queue<int> q;
	for (int i=1; i<=N; i++) {
		vis[i] = false;
		dis[i] = INF;
	}
	vis[1] = true;
	dis[1] = 0;
	//lev[1] = LEVEL[1];
	q.push(1);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (vector<pii>::iterator it=G[u].begin(); it!=G[u].end(); it++) {
			int v = it->first;
			int c = it->second;
			//int l = lev[u]>=0?ABS(lev[u]-LEVEL[v]):0;
			if (dis[v] > dis[u]+c && LEVEL[v] >= start && LEVEL[v] <= end) {
				dis[v] = dis[u]+c;
				if (!vis[v]) {
					vis[v] = true;
					q.push(v);
				}
			}
		}
		vis[u] = false;
	}
}

int main() {
	scanf("%d%d", &M, &N);
	for (int i=1; i<=N; i++) {
		int P, L, X;
		scanf("%d%d%d", &P, &L, &X);
		LEVEL[i] = L;
		VALUE[i] = P;
		for (int j=1; j<=X; j++) {
			int T, V;
			scanf("%d%d", &T, &V);
			G[i].push_back(make_pair(T, V));
		}
	}
	int min = INF;
	for (int i=LEVEL[1]-M; i<=LEVEL[1]; i++) {
		SPFA(i, i+M);
		for (int i=1; i<=N; i++) {
			//printf("%d : %d  %d\n", i, dis[i], dis[i]+VALUE[i]);
			min = MIN(min, dis[i]+VALUE[i]);
		}
		//printf("\n");
	}
	printf("%d\n", min);
	return 0;
}
